package net.shopxx.entity;


import java.math.BigDecimal;
import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.OneToOne;
import javax.persistence.Transient;

import net.shopxx.util.ArithUtil;
import net.shopxx.util.SystemConfigUtil;

import org.compass.annotations.Index;
import org.compass.annotations.SearchableProperty;
import org.compass.annotations.Store;

/**
 * 实体类 - 团购商品类
 */

@Entity
public class Groupbuy extends BaseEntity {
	
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1795188801804290753L;
	private String name;//团购名称
	private String desc;//团购描述
	private Date startTime;//团购开始时间
	private Date endTime;//团购结束时间
	private Product product;//团购的商品
	private FlagshipStore flagshipStore;//商品所属商家
	private Double price;//团购价格 
	private Integer buycount;//最大数量
	private Integer state;//团购状态
	private Boolean recommend;//是否推荐团购
	private Integer view;//查看数
	private String area;//所属城市
	private String areaPath;//所属城市路径
	private Integer type;//团购类型(1.商品团购 2.酒店团购 3.其他团购)
	private Integer category;//团购类别(1.餐饮美食 2.娱乐休闲 3.生活服务 4.生鲜果蔬 5.其他)
	private String htmlFilePath;
	@Column(length = 255)
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	@Column(length = 1024,name="[desc]")
	public String getDesc() {
		return desc;
	}
	public void setDesc(String desc) {
		this.desc = desc;
	}
	public Date getStartTime() {
		return startTime;
	}
	public void setStartTime(Date startTime) {
		this.startTime = startTime;
	}
	public Date getEndTime() {
		return endTime;
	}
	public void setEndTime(Date endTime) {
		this.endTime = endTime;
	}
	@OneToOne(fetch = FetchType.LAZY)
	@JoinColumn(nullable = false)
	public Product getProduct() {
		return product;
	}
	public void setProduct(Product product) {
		this.product = product;
	}
	@OneToOne(fetch = FetchType.LAZY)
	@JoinColumn(nullable = true)
	public FlagshipStore getFlagshipStore() {
		return flagshipStore;
	}
	public void setFlagshipStore(FlagshipStore flagshipStore) {
		this.flagshipStore = flagshipStore;
	}
	@Transient
	public Double getDiscount() {
		return ArithUtil.div(this.price.doubleValue(),this.getProduct().getPrice().doubleValue(),2)*10;
	}
	public Double getPrice() {
		return price;
	}
	public void setPrice(Double price) {
		this.price = price;
	}
	public Integer getBuycount() {
		return buycount;
	}
	public void setBuycount(Integer buycount) {
		this.buycount = buycount;
	}
	public Integer getState() {
		return state;
	}
	public void setState(Integer state) {
		this.state = state;
	}
	public Boolean getRecommend() {
		return recommend;
	}
	public void setRecommend(Boolean recommend) {
		this.recommend = recommend;
	}
	@Column(name="[view]")
	public Integer getView() {
		return view;
	}
	public void setView(Integer view) {
		this.view = view;
	}
	@Column(length = 500)
	public String getAreaPath() {
        return areaPath;
    }
    public void setAreaPath(String areaPath) {
        this.areaPath = areaPath;
    }
    @Column(length = 500)
    public String getArea() {
        return area;
    }
    public void setArea(String area) {
        this.area = area;
    }
    public Integer getType() {
		return type;
	}
	public void setType(Integer type) {
		this.type = type;
	}
	public Integer getCategory() {
		return category;
	}
	public void setCategory(Integer category) {
		this.category = category;
	}
	
	@SearchableProperty(index = Index.NO, store = Store.YES)
	@Column(nullable = true, updatable = false)
	public String getHtmlFilePath() {
		return htmlFilePath;
	}
	public void setHtmlFilePath(String htmlFilePath) {
		this.htmlFilePath = htmlFilePath;
	}
	/**
	 * 获取优惠价格，若member对象为null则返回原价格
	 */
	@Transient
	public BigDecimal getPreferentialPrice(Member member) {
		if (member != null) {
			BigDecimal preferentialPrice = SystemConfigUtil.calculate(price, new Double(member.getMemberRank().getPreferentialScale().toString()));
			return SystemConfigUtil.getPriceScaleBigDecimal(preferentialPrice);
		} else {
			return new BigDecimal(price.toString());
		}
	}
	
	public static void main(String[] args) {
//		new D
	}
	
	
}